package com.manor.main.activity;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.manor.common.activity.AbsActivity;
import com.manor.common.http.JHttpCallback;
import com.manor.main.R;
import com.manor.main.adapter.SdListAdapter;
import com.manor.main.bean.SdListBean;
import com.manor.main.http.JMainHttpUtil;

/**
 * 淘米卷列表
 */

public class SdVolumeActivity extends AbsActivity {
    RecyclerView mRecyclerView;
    SdListAdapter mAdapter;
    private SmartRefreshLayout srlFollow;
    int page = 0;
    String type = "";
    private Gson mGson = new Gson();


    @Override
    protected int getLayoutId() {
        return R.layout.activity_withdrawal;
    }

    @Override
    protected void main() {
        setTitle("嗨券");
        srlFollow = findViewById(R.id.srl_follow);
        mRecyclerView = findViewById(R.id.rc_listview);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        srlFollow.setEnableLoadMore(true);
        srlFollow.setEnableRefresh(true);
        srlFollow.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                setData(false);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                setData(true);
            }
        });
        setData(true);
    }

    private void setData(final boolean istype) {
        if (istype) {
            page = 1;
        } else {
            page++;
        }
        JMainHttpUtil.getCouponsList(page,new JHttpCallback() {
                @Override
                public void onSuccess(int code, String msg, String info) {
                    if (code == 200 && info != null) {
                        SdListBean withdrawalBean = mGson.fromJson(info, SdListBean.class);
                        if (istype) {
                            mAdapter = new SdListAdapter(R.layout.withdrawal_item, withdrawalBean.getRecords(), type);
                            mAdapter.setNewInstance(withdrawalBean.getRecords());
                            mRecyclerView.setAdapter(mAdapter);
                            srlFollow.finishRefresh();
                        } else {
                            mAdapter.addData(withdrawalBean.getRecords());
                            srlFollow.finishLoadMore();
                        }


                    }
                }
            @Override
            public void onError() {
                super.onError();
                srlFollow.finishRefresh();
                srlFollow.finishLoadMore();
            }

            });

    }
}
